package com.czy.tank.game;

import com.czy.tank.common.Dir;
import com.czy.tank.common.Group;
import com.czy.tank.config.PropertyMgr;
import com.czy.tank.config.ResourceMgr;

import java.awt.*;

/********************************************************************
 * @brief 子弹类
 * @version 0.1
 * @date 2022年08月30日 22:22
 * @author changziyang
 ********************************************************************/
public class Bullet {
    //private TankFrame tankFrame;
    private int x,y;
    /*子弹方向*/
    private Dir dir;
    /*定义子弹速度 10*/
    private static final int SPEED = PropertyMgr.getInstance().getInt("bulletSpeed");
    /*定义子弹当前宽高*/
    public int WIDTH;
    public int HEIGHT;
    /*定义子弹存活状态，超出边界或碰撞坦克时死亡*/
    private boolean living = true;
    /*敌我双方分组*/
    private Group group;
    /*当前子弹的矩形边界*/
    public Rectangle rectangle = new Rectangle();
    public Bullet(int x, int y, Dir dir,TankFrame tankFrame,Group group) {
        this.x = x;
        this.y = y;
        this.dir = dir;
        //this.tankFrame = tankFrame;
        this.group = group;
        /*new出子弹同时加入子弹容器*/
        TankFrame.getInstance().bullets.add(this);
    }

    public void paint(Graphics g){
        //画个圆形 模拟子弹
        /*Color c = g.getColor();
        g.setColor(Color.RED);
        g.fillOval(x, y, WIDTH, HEIGHT);
        g.setColor(c);*/

        /*根据方向画出子弹*/
        switch (dir){
            case LEFT:
                g.drawImage(ResourceMgr.bulletL,x,y,null);
                WIDTH = ResourceMgr.bulletL.getWidth();
                HEIGHT = ResourceMgr.bulletL.getHeight();
                break;
            case UP:
                g.drawImage(ResourceMgr.bulletU,x,y,null);
                WIDTH = ResourceMgr.bulletU.getWidth();
                HEIGHT = ResourceMgr.bulletU.getHeight();
                break;
            case RIGHT:
                g.drawImage(ResourceMgr.bulletR,x,y,null);
                WIDTH = ResourceMgr.bulletR.getWidth();
                HEIGHT = ResourceMgr.bulletR.getHeight();
                break;
            case DOWN:
                g.drawImage(ResourceMgr.bulletD,x,y,null);
                WIDTH = ResourceMgr.bulletD.getWidth();
                HEIGHT = ResourceMgr.bulletD.getHeight();
                break;
            default:
                break;
        }
        /*改变边界的宽高*/
        this.rectangle.width = this.WIDTH;
        this.rectangle.height = this.HEIGHT;
        move();
    }

    /*************************************************
    * 方法描述:坦克移动方法
    * @param
    * @return void
    * @author create: 人员:【changziyang】类型:【新增方法】日期:【2022/8/30 22:35】
    * @author modify:
    **************************************************/
    private void move() {
        switch (dir){
            case LEFT:
                x -= SPEED;
                break;
            case UP:
                y -= SPEED;
                break;
            case RIGHT:
                x += SPEED;
                break;
            case DOWN:
                y += SPEED;
                break;
            default:
                break;
        }
        //改变边界的xy
        this.rectangle.x = this.x;
        this.rectangle.y = this.y;
        /*子弹移动超出边界判定为死亡，队列里移除该子弹*/
        if (x<0 || y<0 || x>TankFrame.GAME_WIDTH || y>TankFrame.GAME_HEIGHT){
            this.living = false;
        }
        if (!living){
            TankFrame.getInstance().bullets.remove(this);
        }
    }

    /*************************************************
     * 方法描述:子弹碰撞检测，碰到坦克时 子弹和坦克都销毁
     * @param tank
     * @return void
     * @author create: 人员:【changziyang】类型:【新增方法】日期:【2022/8/31 21:46】
     * @author modify:
     **************************************************/
    public void collideWith(Tank tank) {
        if (this.group == tank.getGroup()){
            //子弹 和坦克属于同一方，不参与碰撞检测
            return;
        }
        //定义坦克爆炸坐标
        int ex = tank.getX() + tank.WIDTH/2 - Explode.WIDTH/2;
        int ey = tank.getY() + tank.HEIGHT/2 - Explode.HEIGHT/2;
        //判定两个矩形是否相交
        if (this.rectangle.intersects(tank.rectangle)){
            this.living = false;
            //如果是主战坦克 碰到敌方子弹，将其置为null及死亡
            if (tank == TankFrame.getInstance().myTank){
                //tankFrame.myTank = null;
                /*添加主战坦克爆炸*/
                TankFrame.getInstance().explodes.add(new Explode(ex,ey,TankFrame.getInstance(),Group.GOOD));
            }else{
                tank.die();
                /*添加敌方坦克爆炸*/
                TankFrame.getInstance().explodes.add(new Explode(ex,ey,TankFrame.getInstance(),Group.BAD));
            }
        }

    }

    public Dir getDir() {
        return dir;
    }

    public void setDir(Dir dir) {
        this.dir = dir;
    }

    public boolean isLiving() {
        return living;
    }

    public void setLiving(boolean living) {
        this.living = living;
    }

    public Group getGroup() {
        return group;
    }

    public void setGroup(Group group) {
        this.group = group;
    }
}
